Sort-merge Join
   HOME

TheInfoList



OR:

The sort-merge join (also known as merge join) is a
join algorithm A join clause in SQL – corresponding to a join operation in relational algebra – combines columns from one or more tables into a new table. Informally, a join stitches two tables and puts on the same row records with matching fields : INNER, ...
and is used in the implementation of a relational
database management system In computing, a database is an organized collection of data stored and accessed electronically. Small databases can be stored on a file system, while large databases are hosted on computer clusters or cloud storage. The design of databases span ...
. The basic problem of a join algorithm is to find, for each distinct value of the join attribute, the set of
tuple In mathematics, a tuple is a finite ordered list (sequence) of elements. An -tuple is a sequence (or ordered list) of elements, where is a non-negative integer. There is only one 0-tuple, referred to as ''the empty tuple''. An -tuple is defi ...
s in each relation which display that value. The key idea of the sort-merge algorithm is to first sort the relations by the join attribute, so that interleaved linear scans will encounter these sets at the same time. In practice, the most expensive part of performing a sort-merge join is arranging for both inputs to the algorithm to be presented in sorted order. This can be achieved via an explicit sort operation (often an
external sort External sorting is a class of sorting algorithms that can handle massive amounts of data. External sorting is required when the data being sorted do not fit into the main memory of a computing device (usually RAM) and instead they must reside in ...
), or by taking advantage of a pre-existing ordering in one or both of the join relations. The latter condition, called interesting order, can occur because an input to the join might be produced by an index scan of a tree-based index, another merge join, or some other plan operator that happens to produce output sorted on an appropriate key. Interesting orders need not be serendipitous: the optimizer may seek out this possibility and choose a plan that is suboptimal for a specific preceding operation if it yields an interesting order that one or more downstream nodes can exploit. Let's say that we have two relations R and S and , R, <, S, . R fits in P_ pages memory and S fits in P_ pages memory. So, in the worst case sort-merge join will run in O(P_+P_) I/Os. In the case that R and S are not ordered the worst case time cost will contain additional terms of sorting time: O(P_+P_+P_\log(P_)+ P_\log(P_)), which equals O(P_\log(P_)+ P_\log(P_)) (as
linearithmic In computer science, the time complexity is the computational complexity that describes the amount of computer time it takes to run an algorithm. Time complexity is commonly estimated by counting the number of elementary operations performed by ...
terms outweigh the linear terms, see Big O notation – Orders of common functions).


Pseudocode

For simplicity, the algorithm is described in the case of an
inner join A join clause in SQL – corresponding to a join operation in relational algebra – combines columns from one or more tables into a new table. Informally, a join stitches two tables and puts on the same row records with matching fields : INNER, ...
of two relations on a single attribute. Generalization to other join types, more relations and more keys is straightforward. function sortMerge(relation left, relation right, attribute a) var relation output var list left_sorted := sort(left, a) ''// Relation left sorted on attribute a'' var list right_sorted := sort(right, a) var attribute left_key, right_key var set left_subset, right_subset ''// These sets discarded except where join predicate is satisfied'' advance(left_subset, left_sorted, left_key, a) advance(right_subset, right_sorted, right_key, a) while not empty(left_subset) and not empty(right_subset) if left_key = right_key ''// Join predicate satisfied'' add cartesian product of left_subset and right_subset to output advance(left_subset, left_sorted, left_key, a) advance(right_subset, right_sorted, right_key, a) else if left_key < right_key advance(left_subset, left_sorted, left_key, a) else ''// left_key > right_key'' advance(right_subset, right_sorted, right_key, a) return output ''// Remove tuples from sorted to subset until the sorted a value changes'' function advance(subset out, sorted inout, key out, a in) key := sorted a subset := emptySet while not empty(sorted) and sorted a = key insert sorted into subset remove sorted


Simple C# implementation

Note that this implementation assumes the join attributes are unique, i.e., there is no need to output multiple tuples for a given value of the key. public class MergeJoin public class Relation


See also

*
Hash join The hash join is an example of a join algorithm and is used in the implementation of a relational database management system. All variants of hash join algorithms involve building hash tables from the tuples of one or both of the joined relations ...
*
Nested loop join A nested loop join is a naive algorithm that joins two sets by using two nested loops. Join operations are important for database management. Algorithm Two relations R and S are joined as follows: algorithm nested_loop_join is for each tup ...


References


External links

C# Implementations of Various Join Algorithms
{{DEFAULTSORT:Sort-Merge Join Join algorithms Articles with example pseudocode Articles with example C Sharp code